# knowledge-library/config.py
from pydantic_settings import BaseSettings
from pydantic import SecretStr, Field, ValidationError
from typing import Optional
import logging

logger = logging.getLogger(__name__)


class Settings(BaseSettings):
    # ChromaDB 配置
    chroma_host: str = Field(
        default="localhost",
        description="ChromaDB服务器地址"
    )
    chroma_port: int = Field(
        default=8000,
        ge=1024,
        le=65535,
        description="ChromaDB服务端口"
    )
    chroma_auth: SecretStr = Field(
        ...,
        description="认证信息（格式：username:password）"
    )
    # 高级配置（可选）
    chroma_ssl: bool = Field(
        default=False,
        description="是否启用HTTPS加密连接"
    )
    # OpenRouter 配置
    openrouter_model: str = Field(
        ...,
        description="默认使用的大模型"
    )
    openrouter_key: SecretStr = Field(
        ...,
        description="OpenRouter API密钥"
    )
    openrouter_base_url:str=Field(
        default="https://openrouter.ai/api/v1",
        description="OpenRouter API基础URL"
    )

    # 应用配置
    chunk_size: int = Field(
        default=500,
        gt=100,
        description="文档分块大小"
    )
    chunk_overlap: int = Field(
        default=50,
        ge=0,
        description="块重叠长度"
    )
    max_file_size: int = Field(
        default=15,  # MB
        description="最大文件上传大小"
    )

    class Config:
        env_file = ".env"
        env_prefix = "KB_"
        case_sensitive = False
        env_file_encoding = 'utf-8'


try:
    settings = Settings()
    print("配置加载成功",settings)
except ValidationError as e:
    logger.critical(f"配置验证失败：{e.errors()}")
    raise RuntimeError("请检查.env文件配置") from e